package com.newy.dao.sys;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Repository;

import com.newy.core.hibernate.HibernateDao;
import com.newy.domain.sys.Menu;
import com.newy.domain.sys.RoleMenu;

/**
 * 角色与菜单关联的dao类
 * @author linzongxue
 *
 */
@Repository
public class RoleMenuDao extends HibernateDao<RoleMenu, Long> {
	
	public List<Menu> getMenusOfRole(Long roleId){
		String hql = "select menu from RoleMenu rm, Menu menu where rm.menuId=menu.id and rm.roleId=?";
		return this.find(hql, roleId);
	}
	
	public void clearRolesFromMenu(Long menuId){
		this.batchExecute("delete from RoleMenu where menuId=?", menuId);
	}
	
	public void clearMenusFromRole(Long roleId){
		this.batchExecute("delete from RoleMenu where roleId=?", roleId);
	}
	
	public void removeRolesFromMenu(Long menuId, Long[] roleIds){
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("menuId", menuId);
		params.put("roleIds", roleIds);
		this.batchExecute("delete from RoleMenu where menuId=:menuId and roleId in :roleIds", params);
	}
	
	public void removeMenusFromRole(Long roleId, Long[] menuIds){
		Map<String, Object> params = new HashMap<String, Object>(2);
		params.put("roleId", roleId);
		params.put("menuIds", menuIds);
		this.batchExecute("delete from RoleMenu where roleId=:roleId and menuId in :menuIds", params);
	}
}
